package com.ysqcould.allocation.service.impl;


import ch.qos.logback.core.joran.conditional.ElseAction;
import cn.hutool.core.util.ObjectUtil;
import com.ysqcould.allocation.domain.Duty;
import com.ysqcould.allocation.domain.WxUser;
import com.ysqcould.allocation.domain.vo.DutyVo;
import com.ysqcould.allocation.mapper.DutyMapper;
import com.ysqcould.allocation.mapper.WxUserMapper;
import com.ysqcould.allocation.service.IDutyService;
import com.ysqcould.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 值班Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-29
 */
@Service
public class DutyServiceImpl implements IDutyService
{
    @Autowired
    private DutyMapper dutyMapper;

    @Resource
    private WxUserMapper userMapper;

    /**
     * 查询值班
     * 
     * @param id 值班主键
     * @return 值班
     */
    @Override
    public Duty selectDutyById(Long id)
    {
        return dutyMapper.selectDutyById(id);
    }

    /**
     * 查询值班列表
     * 
     * @param duty 值班
     * @return 值班
     */
    @Override
    public List<DutyVo> selectDutyList(Duty duty)
    {


        final List<DutyVo> list = dutyMapper.selectDutyList(duty);
        list.forEach(p->{
             if (ObjectUtil.isNotEmpty(p.getDutyPeopleId())){
                 List<String>nameList=new ArrayList<>();
                 final String[] split = p.getDutyLeadId().split(",");
                 for (String s : split) {
                     final WxUser user = userMapper.selectWxUserById(Long.parseLong(s));
                     if (ObjectUtil.isNotEmpty(user)){
                         nameList.add(user.getRealName());
                     }
                     String collect="";
                     if (ObjectUtil.isNotEmpty(nameList)){
                         collect = nameList.stream().collect(Collectors.joining(","));
                     }
                     p.setLearName(collect);
             }
                }
             if (ObjectUtil.isNotEmpty(p.getDutyPeopleId())){
                 final String[] split1 = p.getDutyPeopleId().split(",");
                 List<String>pList=new ArrayList<>();
                 for (String s : split1) {
                     final WxUser user = userMapper.selectWxUserById(Long.parseLong(s));
                     if (ObjectUtil.isNotEmpty(user)){
                         pList.add(user.getRealName());
                     }
                     String str="";
                     if (ObjectUtil.isNotEmpty(pList)){
                           str = pList.stream().collect(Collectors.joining(","));
                     }
                     p.setPeopleNme(str);
                 }
             }
        });
        return list;
    }

    /**
     * 新增值班
     * 
     * @param duty 值班
     * @return 结果
     */
    @Override
    public int insertDuty(Duty duty)
    {
        duty.setCreateTime(DateUtils.getNowDate());
        return dutyMapper.insertDuty(duty);
    }

    /**
     * 修改值班
     * 
     * @param duty 值班
     * @return 结果
     */
    @Override
    public int updateDuty(Duty duty)
    {
        duty.setUpdateTime(DateUtils.getNowDate());
        return dutyMapper.updateDuty(duty);
    }

    /**
     * 批量删除值班
     * 
     * @param ids 需要删除的值班主键
     * @return 结果
     */
    @Override
    public int deleteDutyByIds(Long[] ids)
    {
        return dutyMapper.deleteDutyByIds(ids);
    }

    /**
     * 删除值班信息
     * 
     * @param id 值班主键
     * @return 结果
     */
    @Override
    public int deleteDutyById(Long id)
    {
        return dutyMapper.deleteDutyById(id);
    }

    @Override
    public int updateStatus(Duty duty) {
        Duty dutyById = dutyMapper.selectDutyById(duty.getId());
        dutyById.setStatus(duty.getStatus());
        return dutyMapper.updateDuty(dutyById);
    }
}
